﻿//********************************************************************************//
//创建日期：<创建日期，2016-04-08>
//创建作者：<朱大鹏，zhudapeng@shanghai3h.com>
//功能说明：汇总
//********************************************************************************//
using System;
using Entity;
using BLL;
using BLL.Access;
using System.Data;
using Platform.Common;

namespace Web.Pages.YongShui._4.ChaoBiaoKZ
{
    public partial class HuiZong : BasePage
    {
        #region 属性
        public DataTable MyDataTable
        {
            get { return ViewState["MyDataTable"] != null ? ViewState["MyDataTable"] as DataTable : null; }
            set { ViewState["MyDataTable"] = value; }
        }

        #endregion

        #region 变量
        /// <summary>
        /// 数据库
        /// </summary>
        SpHelper _spHelper = new SpHelper();
        #endregion 

        #region 事件
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                InitControl();
               
            }
           
        }
       
        /// <summary>
        /// 查错按钮点击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void BtnSelectError_Click(object sender, EventArgs e)
        {
            BindErrorData();
        }
        /// <summary>
        /// 汇总按钮点击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void BtnHUIZONG_Click(object sender, EventArgs e)
        {
            BindHZData();
        }
        
        
        #endregion

        #region  方法
        /// <summary>
        /// 初始化控件  并绑定值
        /// </summary>
        private void InitControl()
        {
           
            string yd = BusinessTool.YS_GETSETTINGSVALUE(eSETTINGS_KEYNAME.WATER_Y_M.ToString());
            IYIM.Text = string.Format("{0}年{1}月",yd.Substring(0, 4),yd.Substring(4, 2)); 

            //获取当前用户站点信息
            string zhandianBH = BusinessTool.ZHANDIANBH();

            YS_JX_GUANLIKBLL glkbll = new YS_JX_GUANLIKBLL();

            DataTable glkdt = glkbll.SelectYS_JX_GUANLIKsDynamic(" I_JLZT=0 and (S_ZHANDIAN='" + zhandianBH + "' or '00'='" + zhandianBH + "')", " S_GUANLIKH");

            DrpQK.DataSource = glkdt;
            DrpQK.TextField = "S_GUANLIKMC";
            DrpQK.ValueField = "S_GUANLIKH";
            DrpQK.DataBind();

            if (zhandianBH == "00")
            {
                DrpQK.Items.Insert(0,new DevExpress.Web.ListEditItem("所有区块","0"));
            }
            
            DrpQK.SelectedIndex = 0;
            if (glkdt != null && glkdt.Rows.Count > 0)
            {
                string ids = "";
                foreach (DataRow dr in glkdt.Rows)
                {
                    ids += dr["S_GUANLIKH"] + ",";
                }
                glklist.Value = ids;
            }
           
            txtZhuanHuanJG.Visible = true;
            BtnHUIZONG.ClientEnabled = false;
        }
       
        
        /// <summary>
        /// 查错
        /// </summary>
        private void BindErrorData()
        {
           
            txtZhuanHuanJG.Visible = true;
            string yd = BusinessTool.YS_GETSETTINGSVALUE(eSETTINGS_KEYNAME.WATER_Y_M.ToString());

            YS_YG_YONGHUXXBLL yhxxbll = new YS_YG_YONGHUXXBLL();
           

            YS_CB_HUIZONGCWBLL hzxxbll = new YS_CB_HUIZONGCWBLL();
            string SGUANLIKH = "";
            if (DrpQK.Value == null)
            {
                txtZhuanHuanJG.Text = "管理块不存在";
                return;
            }
            
            SGUANLIKH = DrpQK.Value.ToString();
            
            
            DataTable dtI_GUANBIAO = WordsHelper.GetXiaoLeiListByWordsID(10);
            int StartMonth = 0;
            int EndMnth = 0;
            if (dtI_GUANBIAO != null && dtI_GUANBIAO.Rows.Count > 0)
            {
                foreach (DataRow dr in dtI_GUANBIAO.Rows)
                {
                    if (dr["WordsContent"].ToString().Trim() == "冬灌表起始月")
                    {
                        StartMonth = Convert.ToInt32(dr["WordsValue"]);
                    }
                    if (dr["WordsContent"].ToString().Trim() == "冬灌表中止月")
                    {
                        EndMnth = Convert.ToInt32(dr["WordsValue"]);
                    }
                }
            }
            int syear = 0;
            int smonth = 0;
            
            if (yd != null && yd.Trim().Length == 6)
            {
                syear = Convert.ToInt32(yd.Substring(0, 4));
                smonth = Convert.ToInt32(yd.Substring(4, 2));
            }
            string ShowMsg = "";
            int? errcode = 0;
            DataSet ds = _spHelper.P_YS_CB_HUIZONGCW(SGUANLIKH, syear, smonth, StartMonth, EndMnth, 0,out errcode);
            DataTable dt = ds.Tables[0];

            if (dt != null && dt.Rows.Count > 0)
            {
                txtZhuanHuanJG.Text = dt.Rows[0][0].ToString().Replace("。", "\r\n");
                //return;
                //List<string> arrErrorName = new List<string>();
                //List<int> arrErrorCount = new List<int>();
                //foreach (DataRow dr in dt.Rows)
                //{
                //    if (dr["S_CUOWUXX"].ToString().Trim().Length > 0)
                //    {
                //        if (!arrErrorName.Contains(dr["S_CUOWUXX"].ToString().Trim()))
                //        {
                //            arrErrorName.Add(dr["S_CUOWUXX"].ToString());
                //            arrErrorCount.Add(1);
                //        }
                //        else
                //        {
                //            int index = arrErrorName.IndexOf(dr["S_CUOWUXX"].ToString().Trim());
                //            arrErrorCount[index] += 1;
                //        }
                //    }
                //}

                //for (int i = 0; i < arrErrorName.Count; i++)
                //{
                //    if (arrErrorName[i] != null && arrErrorName[i].Trim().Length > 0)
                //    {
                //        ShowMsg += arrErrorName[i] + "共(" + arrErrorCount[i] + ")笔。 ";
                //    }
                //}
                //txtZhuanHuanJG.Text = ShowMsg == "" ? "查错完成,无错误信息" : ShowMsg;
            }
            else
            {
                
            }
            if (errcode == 0)
            {
                //txtZhuanHuanJG.Text = "无错误信息";
                BtnHUIZONG.Enabled = true;
            }
            else
            {
                BtnHUIZONG.Enabled = true;
            }
        }
        /// <summary>
        /// 查错
        /// </summary>
        private string CheckError(string glk,out int error)
        {
           
            string yd = BusinessTool.YS_GETSETTINGSVALUE(eSETTINGS_KEYNAME.WATER_Y_M.ToString());
            YS_CB_HUIZONGCWBLL hzxxbll = new YS_CB_HUIZONGCWBLL();           


            DataTable dtI_GUANBIAO = WordsHelper.GetXiaoLeiListByWordsID(10);
            int StartMonth = 0;
            int EndMnth = 0;
            if (dtI_GUANBIAO != null && dtI_GUANBIAO.Rows.Count > 0)
            {
                foreach (DataRow dr in dtI_GUANBIAO.Rows)
                {
                    if (dr["WordsContent"].ToString().Trim() == "冬灌表起始月")
                    {
                        StartMonth = Convert.ToInt32(dr["WordsValue"]);
                    }
                    if (dr["WordsContent"].ToString().Trim() == "冬灌表中止月")
                    {
                        EndMnth = Convert.ToInt32(dr["WordsValue"]);
                    }
                }
            }
            int syear = 0;
            int smonth = 0;

            if (yd != null && yd.Trim().Length == 6)
            {
                syear = Convert.ToInt32(yd.Substring(0, 4));
                smonth = Convert.ToInt32(yd.Substring(4, 2));
            }
            string ShowMsg = "";
            int? errcode = 0;
            DataSet ds = _spHelper.P_YS_CB_HUIZONGCW(glk, syear, smonth, StartMonth, EndMnth, 0, out errcode);
            DataTable dt = ds.Tables[0];

            if (dt != null && dt.Rows.Count > 0)
            {
                ShowMsg = dt.Rows[0][0].ToString().Replace("。", "\r\n").Replace("无错误信息，可以汇总","").Replace("本管理块无用户信息;","");
                
            }
            error = Convert.ToInt32(errcode==-10?0:errcode);
            return ShowMsg;
        }
        /// <summary>
        /// 汇总
        /// </summary>
        private void BindHZData()
        {
            YS_CB_HUIZONGCWBLL hzxxbll = new YS_CB_HUIZONGCWBLL();
            DataTable dtI_GUANBIAO = WordsHelper.GetXiaoLeiListByWordsID(10);
            int StartMonth = 0;
            int EndMnth = 0;
            if (dtI_GUANBIAO != null && dtI_GUANBIAO.Rows.Count > 0)
            {
                foreach (DataRow dr in dtI_GUANBIAO.Rows)
                {
                    if (dr["WordsContent"].ToString().Trim() == "冬灌表起始月")
                    {
                        StartMonth = Convert.ToInt32(dr["WordsValue"]);
                    }
                    if (dr["WordsContent"].ToString().Trim() == "冬灌表中止月")
                    {
                        EndMnth = Convert.ToInt32(dr["WordsValue"]);
                    }
                }
            }
            string SGUANLIKH = "-1";
            
            SGUANLIKH = DrpQK.Value.ToString();
            

            int syear = 0;
            int smonth = 0;
            string yd = BusinessTool.YS_GETSETTINGSVALUE(eSETTINGS_KEYNAME.WATER_Y_M.ToString());
            if (yd != null && yd.Trim().Length == 6)
            {
                syear = Convert.ToInt32(yd.Substring(0, 4));
                smonth = Convert.ToInt32(yd.Substring(4, 2));
            }
            string msg = "";
            if (DrpQK.Value.ToString() == "0")
            {
                foreach (DevExpress.Web.ListEditItem li in DrpQK.Items)
                {
                    if (li.Value.ToString() != "0")
                    {
                        DataSet ds = _spHelper.P_YS_CB_HUIZONGTJ(li.Value.ToString(), syear, smonth, StartMonth, EndMnth, LoginInfo.CurrentUser.Account);
                        DataTable dt = ds.Tables[0];
                        if (dt != null && dt.Rows.Count > 0)
                        {
                            msg += li.Text + "：" + dt.Rows[0][0].ToString() + "\r\n";
                        }
                    }
                }
            }
            else
            {
                DataSet ds = _spHelper.P_YS_CB_HUIZONGTJ(DrpQK.Value.ToString(), syear, smonth, StartMonth, EndMnth, LoginInfo.CurrentUser.Account);
                DataTable dt = ds.Tables[0];
                if (dt != null && dt.Rows.Count > 0)
                {
                    msg += DrpQK.Text + "：" + dt.Rows[0][0].ToString() + "\r\n";
                }
            }
            //DataSet ds = _spHelper.P_YS_CB_HUIZONGTJ(SGUANLIKH, syear, smonth, StartMonth, EndMnth, LoginInfo.CurrentUser.Account);
            //DataTable dt = ds.Tables[0];
            //if (dt != null && dt.Rows.Count > 0)
            //{
            //    txtZhuanHuanJG.Text = dt.Rows[0][0].ToString();
            //}
            txtZhuanHuanJG.Text = msg;
        }
        private string SumData(string glk)
        {
            YS_CB_HUIZONGCWBLL hzxxbll = new YS_CB_HUIZONGCWBLL();
            DataTable dtI_GUANBIAO = WordsHelper.GetXiaoLeiListByWordsID(10);
            int StartMonth = 0;
            int EndMnth = 0;
            if (dtI_GUANBIAO != null && dtI_GUANBIAO.Rows.Count > 0)
            {
                foreach (DataRow dr in dtI_GUANBIAO.Rows)
                {
                    if (dr["WordsContent"].ToString().Trim() == "冬灌表起始月")
                    {
                        StartMonth = Convert.ToInt32(dr["WordsValue"]);
                    }
                    if (dr["WordsContent"].ToString().Trim() == "冬灌表中止月")
                    {
                        EndMnth = Convert.ToInt32(dr["WordsValue"]);
                    }
                }
            }

            int syear = 0;
            int smonth = 0;
            string yd = BusinessTool.YS_GETSETTINGSVALUE(eSETTINGS_KEYNAME.WATER_Y_M.ToString());
            if (yd != null && yd.Trim().Length == 6)
            {
                syear = Convert.ToInt32(yd.Substring(0, 4));
                smonth = Convert.ToInt32(yd.Substring(4, 2));
            }
            string msg = "";

            DataSet ds = _spHelper.P_YS_CB_HUIZONGTJ(glk, syear, smonth, StartMonth, EndMnth, LoginInfo.CurrentUser.UserID.ToString());
            DataTable dt = ds.Tables[0];
            DrpQK.Value = glk;

            if (dt != null && dt.Rows.Count > 0)
            {
                if (dt.Rows[0][0].ToString().IndexOf(DrpQK.Text) > -1)
                {
                    msg =  dt.Rows[0][0].ToString() + "\r\n";
                }
                else
                {
                    msg = DrpQK.Text + "：" + dt.Rows[0][0].ToString() + "\r\n";
                }
            }
            return msg;
        }
        #endregion

        protected void ASPxCallback1_Callback(object source, DevExpress.Web.CallbackEventArgs e)
        {
            if (e.Parameter == "checkerror")
            {

                int code = -1;
                string msg = "";
                int error = 0;
                string newids = "";
                if (HiddenList.Contains("glkcheck"))
                {
                    string glk = HiddenList.Get("glkcheck").ToString().Split(',')[0];
                    string[] ids = HiddenList.Get("glkcheck").ToString().Split(',');

                    if (glk.Trim().Length > 0)
                    {
                        foreach (string s in ids)
                        {
                            if (s.Trim() != glk.Trim())
                            {
                                newids += s + ",";
                            }
                        }
                        msg = CheckError(glk,out error);
                        code = 1;
                    }
                    else
                    {
                        code = 2;
                    }
                }
                e.Result = e.Parameter + "|" + code + "|" + msg + "|" + newids+"|"+error;
            }
            if (e.Parameter == "sumdata")
            {
                int code = -1;
                string msg = "";
               
                string newids = "";
                if (HiddenList.Contains("glk"))
                {
                    string glk = HiddenList.Get("glk").ToString().Split(',')[0];
                    string[] ids = HiddenList.Get("glk").ToString().Split(',');

                    if (glk.Trim().Length > 0)
                    {
                        foreach (string s in ids)
                        {
                            if (s.Trim() != glk.Trim())
                            {
                                newids += s + ",";
                            }
                        }
                        msg = SumData(glk);
                        code = 1;
                    }
                    else
                    {
                        code = 2;
                    }
                }
                e.Result = e.Parameter + "|" + code + "|" + msg + "|" + newids;
            }
        }

    }
}